package iyoga;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class User {
	private String user_name;//用户名
	private String address;//地址
	private int user_id;//用户id
	private String job ;//工作
	private int state;//用户状态，是否有效
	

	
	public String getUser_name() {
		return user_name;
	}
	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public int getUser_id() {
		return user_id;
	}
	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	//用户是否已经注册
    static Boolean is_register (String name){
		Boolean res=false;
		String name_sql="select user_name from user";
		Connection conn=JDBCUtil.getConnection();
		Statement stmt = null;
		try {
			stmt = conn.createStatement();
			ResultSet rs=stmt.executeQuery(name_sql);
			while(rs.next()){
				if(rs.getString("user_name").equals(name)){
					res=true;
					break;
				};
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return res;
	}
    //添加用户
    public static void add_member(String name,String address,String job){
		//检测用户名是否被注册，保证用户名唯一
		if(is_register(name)){
			System.out.println("sorry!your username has been register!");
			return;
		}
		Connection conn=JDBCUtil.getConnection();
		String insertsql="insert into user(user_name,address,job) values('"+name+"','"+address+"' ,' "+job+"')";
		Statement stmt;
		try {
			stmt = conn.createStatement();
			stmt.executeUpdate(insertsql);
			ResultSet rs=stmt.executeQuery("select user_id from user where user_name='"+name+"'");
			String this_id="";
			while(rs.next()){
				this_id=rs.getString("user_id");
			}
			System.out.println("ok! "+name+"'s member id is "+this_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		
	}
}
